var saveDHC = require("./../dhc/saveDHC");
//头部
var exports = {};

var element = null;

var panel = $(".designerOperation");

exports.init = function() {
	$(".allNew").on("click", function(){
		Design.mainEditer.product.reset();
	});
	$(".shortcut-a").on("click", function(){
		$(".shortCutsDiv").fadeIn();
	});
	$(".shortCutsDiv .icon-guanbi").on("click", function(){
		$(".shortCutsDiv").fadeOut();
	});
	panel.find("a").on("click", function() {
		panel.find("a").removeClass("cur");
		$(this).addClass("cur");
		var type = $(this).data("type");
		switch (type) {
			case "text":
				addTextToStage();
				break;
			case "img":
				var rect = Design.mainEditer.getEditeRect();
				if(rect){
					$("#uploadimgInput").click();
				}
				break;
			case "product": //产品列表
				$(".chooseProduct,.chooseDesign").hide();
				$(".chooseProduct").fadeIn();
				break;
			case "design": //图库列表
				$(".chooseProduct,.chooseDesign").hide();
				$(".chooseDesign").fadeIn();
				Design.view.img.updateView("img");
				break;
			case "bg": //图库列表
				$(".chooseProduct,.chooseDesign").hide();
				$(".chooseDesign").fadeIn();
				Design.view.img.updateView("bg");
				break;
			default:
				break;
		}
	});

	$("#saveBtn").on("click", exports.doSave);
}

var uploadindex = null;
$("#uploadimgInput").on("change", function() {
	var file = this.files[0];
	var url = config_gl.apiPath + "upload/image";
	var formData = new FormData();
	formData.append('images', file);
	uploadindex = layer.load();
	$.ajax({
		url: url,
		type: "post",
		data: formData,
		contentType: false,
		processData: false,
		mimeType: "multipart/form-data",
		success: function(data) {
			if (typeof data == "string") {
				data = JSON.parse(data);
			}
			addImagToStage(data.data[0]);
			uploadimgToTuku(data.data[0]);
		}
	})
});

function uploadimgToTuku($data) {
	$data.filed = 0;
	$data.tag = "";
	$data.galleryCategoryId = "";
	var url = config_gl.apiPath + "gallery/add";
	$.ajax({
		url: url,
		type: "post",
		contentType: "application/json",
		data: JSON.stringify({
			images: [$data]
		}),
		success: function(data) {
			console.log(data);
			if(uploadindex){
				layer.close(uploadindex);
				uploadindex = null;
			}
		}
	})
}

function addTextToStage() {
	var rect = Design.mainEditer.getEditeRect();
	if(rect){
		var tx = rect.x + rect.width / 2 - 160 / 2;
		var ty = rect.y + rect.height / 2 - 36 / 2;
		var data = {
			type: "text",
			x: tx,
			y: ty
		}
		Design.event.trigger(Design.event.ADD_ELEMENT, data);
	}
}

/* 将图片添加到舞台 */
function addImagToStage($data) {
	var rect = Design.mainEditer.getEditeRect();
	if(rect){
		var tx = rect.x + rect.width / 2 - $data.width / 2;
		var ty = rect.y + rect.height / 2 - $data.height / 2;
		var realScale = Design.mainEditer.product.realScale || 1;
		var scale = 1 / realScale;
		if($data.width*scale > rect.width){
			scale = rect.width / $data.width;
			tx = rect.x;
			ty = rect.y;
		}
		var data = {
			type: "img",
			url: $data.fileUrl,
			scalex: scale,
			scaley: scale,
			x: tx,
			y: ty,
			name: $data.name
		}
		Design.event.trigger(Design.event.ADD_ELEMENT, data);
	}
}

/* 保存 */
exports.doSave = function() {
	saveDHC.doSave();
}


exports.bind = function($ele) {
	element = $ele;
}

exports.unbind = function() {
	element = null;
}

module.exports = exports;
